import Vue from 'vue'
import Router from 'vue-router'
import WRouter from './router-w'
import MRouter from './router-m'
import CRouter from './router-cms'

import Tool from '../../src/page/cms/tool/Tool'
import ETHTool from '../../src/page/cms/tool/ETHTool'

Vue.use(Router);

let router = new Router({
    routes: [{
        path: '/tool',
        name: 'Tool',
        component: Tool,
        meta: {title: "tool", authCMS: true, boot: false}
    },{
        path: '/eth_tool',
        name: 'ETHTool',
        component: ETHTool,
        meta: {title: "ETHtool", authCMS: true, boot: false}
    },
        ...WRouter.router,
        ...MRouter.router,
        ...CRouter.router,
    ]
});

router.beforeEach((to, from, next) => {
    //判断是否有标题
    if (to.meta.title) {
        document.title = to.meta.title
    }

    let tokenCMS = localStorage.getItem('sid');
    let tokenWeb = router.app.$cookies.get('sid');
    let is_mobile = navigator.userAgent.toLowerCase().match(
        /(ipod|ipad|iphone|android|coolpad|mmp|smartphone|midp|wap|xoom|symbian|j2me|blackberry|wince)/i
    ) != null;

    //cms 判断是否需要登陆
    if (to.meta.authCMS) {
        if (to.path === '/cms/login') {
            next();
        } else if (to.path === '/tool') {
            next()
        } else {
            if (tokenCMS === 'null' || tokenCMS === '' || tokenCMS === null) {
                next('/cms/login');
            } else {
                next();
            }
        }
    }
    // //web 判断用户是 pc 还是 mobile 打开，进行重定向
    // if (to.meta.boot) {
    //     if (to.path.slice(0, 2) === "/w") {
    //         if (is_mobile) {
    //             next('/m' + to.path.slice(2));
    //         }
    //     } else if (to.path.slice(0, 2) === "/m") {
    //         if (!is_mobile) {
    //             next('/w' + to.path.slice(2));
    //         }
    //     }
    // }
    //web 进入首页判断 pc 还是 mobile，进行重定向
    if (to.meta.authWEB) {
        if (to.path === '/m/login' || to.path === '/m/register' || to.path === '/m/forget') {
            next();
        }else{
            if (tokenWeb === 'null' || tokenWeb === '' || tokenWeb === null) {
                next('/m/login');
            } else {
                if (to.path === '/') {
                    if (is_mobile) {
                        next('/m/home');
                    } else {
                        next('/m/home');
                    }
                } else {
                    next()
                }
            }
        }
    }
});

export default router;
